﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    public class Curve : Entity
    {
        #region API 函数
        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="curve"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUCurveRelease(IntPtr curve);

        /// <summary>
        /// 曲线类型
        /// </summary>
        /// <param name="curve"></param>
        /// <param name="type">0：一般曲线；1：圆弧</param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUCurveGetType(IntPtr curve, out int type);

        /// <summary>
        /// 使用一组edge创建一个曲线，要求这些edge不属于某个face
        /// </summary>
        /// <param name="curve"></param>
        /// <param name="edges"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUCurveCreateWithEdges(out IntPtr curve, IntPtr[] edges, long len);

        /// <summary>
        /// 曲线包含的边缘数量
        /// </summary>
        /// <param name="curve"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUCurveGetNumEdges(IntPtr curve, out long count);

        /// <summary>
        /// 获取全部边缘
        /// </summary>
        /// <param name="curve"></param>
        /// <param name="count"></param>
        /// <param name="edges"></param>
        /// <param name="realCount"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUCurveGetEdges(IntPtr curve, long count, IntPtr[] edges, out long realCount);

        /// <summary>
        /// 是否是多边形
        /// </summary>
        /// <param name="curve"></param>
        /// <param name="isCurve"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUCurveIsPolygon(IntPtr curve, out bool isCurve);
        #endregion

        /// <summary>
        /// 是否是圆弧
        /// </summary>
        public bool IsArc
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUCurveGetType(Id, out int t));
                return t == 1;
            }
        }

        /// <summary>
        /// 是否是多边形
        /// </summary>
        /// <remarks>只有该曲线最初是由多边形工具创建时才是 true，否则为 false</remarks>
        public bool IsPolygon
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUCurveIsPolygon(Id, out bool isPolygon));
                return isPolygon;
            }
        }

        /// <summary>
        /// 全部边缘
        /// </summary>
        public List<Edge> Edges
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUCurveGetNumEdges(Id, out long count));
                IntPtr[] ptrs = new IntPtr[count];
                SketchUpExceptionHelper.ThrowSUResult(SUCurveGetEdges(Id, count, ptrs, out long realCount));
                return ptrs.Select(pt => new Edge(pt)).ToList();
            }
        }

        internal Curve(IntPtr id) : base(id)
        {

        }

        /// <summary>
        /// 创建曲线
        /// </summary>
        /// <param name="edges"></param>
        public Curve(params Edge[] edges)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUCurveCreateWithEdges(out Id, edges.Select(e => e.Id).ToArray(), edges.Length));
        }

        /// <summary>
        /// 释放 <see cref="Curve"/> 资源，及其包含的全部 <see cref="Edge"/>
        /// </summary>
        public override void Dispose()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUCurveRelease(Id));
        }
    }
}